/**
 * 
 */
package database.local;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.google.android.maps.GeoPoint;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import server.SaveZoneHttpConnection;
import server.ServerObject;
import server.XmlParser;

/**
 * @author shiko
 *
 */
public class Business extends ServerObject implements Comparable<Business>, Parcelable {
	private String name;
	private String country;
	private String city;
	private String street;
	private int streetNum;
	private String fullAddress;
	private String type; /* pub or rest*/
	private String website;
	private String pic_link;
	private String more_info;
	private Integer latitude;
	private Integer longitude;
	private List<Deal> deals;
	private int distance; 
	
	public Business() {
		super();
		setName("");
		setCity("");
		setStreet("");
		setStreetNum(0);
		setFullAddress("");
		setType("");
		setWebsite("");
		setCountry("");
		setPic_link("");
		setDeals(new ArrayList<Deal>());
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public int getStreetNum() {
		return streetNum;
	}

	public void setStreetNum(int streetNum) {
		this.streetNum = streetNum;
	}

	public String getFullAddress() {
		
		if (fullAddress == null || fullAddress == ""){
			fullAddress = "";
			
			if (getCountry() != null){
				fullAddress += getCountry();
				fullAddress += " ";
			}
			
			if (getCity() != null){
				fullAddress += getCity();
				fullAddress += " ";
			}
			
			if (getStreet() != null) {
				fullAddress += getStreet();
				fullAddress += " ";
			}
			
			if (getStreetNum() > 0) {
				fullAddress += Integer.toString(getStreetNum());
				fullAddress += " ";
			}
			
		}
		
		return fullAddress.trim();
	}

	public void setFullAddress(String fullAddress) {
		this.fullAddress = fullAddress;
	}

	public Integer getLatitude() {
		return latitude;
	}

	public void setLatitude(Integer latitude) {
		this.latitude = latitude;
	}

	public Integer getLongitude() {
		return longitude;
	}

	public void setLongitude(Integer longitude) {
		this.longitude = longitude;
	}

	/**
	 * @return the geoPoint
	 */
	public GeoPoint getGeoPoint() {
		if (longitude == null || latitude == null) return null;
		return new GeoPoint(latitude.intValue(), longitude.intValue());
	}

	/**
	 * @param geoPoint the geoPoint to set
	 */
	public void setGeoPoint(GeoPoint geoPoint) {
		if (geoPoint == null) return;
		this.latitude = new Integer(geoPoint.getLatitudeE6());
		this.longitude =new Integer(geoPoint.getLongitudeE6());
	}


	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	/**
	 * @return the more_info
	 */
	public String getMore_info() {
		return more_info;
	}

	/**
	 * @param more_info the more_info to set
	 */
	public void setMore_info(String more_info) {
		this.more_info = more_info;
	}

	/**
	 * 
	 * @return sorted list by rating of deals.
	 */
	public List<Deal> getDeals() {
		if (deals == null) return null;
		Collections.sort(deals);
		return deals;
	}

	public void setDeals(List<Deal> deals) {
		this.deals = deals;
	}
	
	/**
	 * @return the distance in meters
	 */
	public int getDistance() {
		return distance;
	}

	/**
	 * @param d the distance to set in meters
	 */
	public void setDistance(int d) {
		this.distance = d;
	}

	public String toXml() {
		return XmlParser.businessToXml(this);
	}
	
	public void checkInMe(String user_name, String email, String pic, String facebook_link) {
		SaveZoneHttpConnection.checkIn(this, user_name, email, pic, facebook_link);
	}
	
	/*compared by rating of the max deal rate*/
	public int compareTo(Business another) {
		if (another == null) return -1;
		Deal this_deal = this.getDealMaxRate();
		Deal another_deal = another.getDealMaxRate();
		if (this_deal == null) return 1;
		if (another_deal == null) return -1;
		int compare = this_deal.compareTo(another_deal);
		return compare;
	}
	
	/**
	 * 
	 * @return the deal with the max rate.
	 * if there is no deal return null.
	 */
	public Deal getDealMaxRate() {
		List<Deal> deals = getDeals();
		if (deals == null || deals.size() == 0) return null;
		Collections.sort(deals);
		return deals.get(0);
	}
	
	/**
	 * used to add or to update the remote data base  
	 * @return false if failed
	 * @param user can be changed to user_name
	 * 
	 */
	public boolean syncWithMainDataBase(User user) {
		List<Business> business_lst = new LinkedList<Business>();
		business_lst.add(this);
		InputStream stream = SaveZoneHttpConnection.add(business_lst, user);
		
		if (stream == null) return false;
		
		/*check if failed*/
		return SaveZoneHttpConnection.checkResult(stream);
	}
	
	/* Parcelable implements methods */
	
	public Business (Parcel in){
		super();
		readFromParcel(in);
	}
	
	public void readFromParcel(Parcel in) {
		super.readFromParcel(in);
		name = in.readString();
		country = in.readString();
		city = in.readString();
		street = in.readString();
		streetNum = in.readInt();
		int temp = in.readInt();
		latitude = ( (temp == 0) ? null : new Integer(temp));
		temp = in.readInt();
		longitude = ( (temp == 0) ? null : new Integer(temp));
		distance = in.readInt();
		fullAddress = in.readString();
		type = in.readString();
		website = in.readString();
		more_info = in.readString();
		pic_link = in.readString();
		if (deals == null){
			deals = new ArrayList<Deal>();
		}
		
		in.readTypedList(deals,Deal.CREATOR);
	}

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		Log.i("Business","Writing: " + this.name + ", with: " + deals.size() + " deals" );
		super.writeToParcel(dest, flags);
		dest.writeString(name);
		dest.writeString(country);
		Log.i("Business", city);
		dest.writeString(city);
		dest.writeString(street);
		dest.writeInt(streetNum);
		dest.writeInt(latitude!=null?latitude.intValue(): 0);
		dest.writeInt(longitude!=null?longitude.intValue(): 0);
		dest.writeInt(distance);
		dest.writeString(fullAddress);
		dest.writeString(type); /* pub or rest*/
		dest.writeString(website);
		dest.writeString(more_info);
		dest.writeString(pic_link);
		dest.writeTypedList(deals);
		
	}
	

	public String getPic_link() {
		return pic_link;
	}

	public void setPic_link(String pic_link) {
		this.pic_link = pic_link;
	}


	public static final Parcelable.Creator CREATOR =
	    	new Parcelable.Creator() {
	            public Business createFromParcel(Parcel in) {
	                return new Business(in);
	            }
	 
	            public Business[] newArray(int size) {
	                return new Business[size];
	            }
	        };

}
